AddCSLuaFile("cl_menu.lua")
AddCSLuaFile("donation_menu.lua")
AddCSLuaFile( "autorun/autorun.lua" )
local tab_plugins = file.FindInLua("agm_tabs/*.lua")
for _, filename in pairs( tab_plugins ) do
	Msg( "Loading: " .. filename .. "\n" )
	AddCSLuaFile( "agm_tabs/" .. filename )
end
//Handle ranks for players
local RankTable = {}

//Saving and loading ranks
function LoadUserRanks()
	RankTable = {}
	
	if file.Exists( "AGM/userranks.txt" ) then
		local tfile = file.Read( "AGM/userranks.txt" )
		local entries = string.Explode( "\n", tfile )
		
		for _, v in pairs(entries) do
			if string.len( string.Trim(v) ) > 0 then
				local pars = string.Explode( " ", v )
				local entry = {}
				entry.SteamID = pars[1]
				entry.Rank = table.concat(pars, " ", 2)
				
				table.insert( RankTable, entry )
			end
		end
	else
	end
end
if SERVER then
	LoadUserRanks()
	concommand.Add( "ReloadRanks", LoadRanks )
end
local Player = FindMetaTable("Player")
function GetRank( ply )
	for k, v in pairs(RankTable) do
		if v.SteamID = ply:SteamID() then return v.Rank end
	end
	return "Guest"
end

function SaveUserRanks()
	if !SERVER then return false end
	
	local tfile = ""
	
	for _, v in pairs( RankTable ) do
		tfile = tfile .. v.SteamID .. " " .. v.Rank .. "\n"
	end
	
	file.Write( "AGM/userranks.txt", tfile )
end
//Table that holds all rank information
Ranks = {}

function RankExists( Rank )
	for _, v in pairs(Ranks) do
		if string.lower(v.Title) == string.lower(Rank) then return v.Title end
	end
end

//Parse ranks from string
function ParseRanks( FileStr )
	if !SERVER then return false end

	for _, r in pairs( string.Explode("\n", FileStr) ) do
		local RankRaw = string.Explode("|", r)
		if !RankExists( RankRaw[1] ) then
			local Rank = {}
			Rank.Title = RankRaw[1]
			Rank.Privileges = string.Explode(":", RankRaw[2])
			table.insert( Ranks, Rank )
		else
			for _, rr in pairs( Ranks ) do
				if rr.Title == RankRaw[1] then
					for _, p in pairs( string.Explode(":", RankRaw[2]) ) do
						table.insert( rr.Privileges, p )
					end
					break
				end
			end
		end
	end
end

//Loading and saving ranks on the server
//On the client we don't load ranks from a file.
function LoadRanks()
	local rankfiles = file.Find( "AGM/ranks/*.txt" )
	if #rankfiles > 0 then
		//Load the user rank files
		for _, f in pairs( rankfiles ) do
			ParseRanks( file.Read("AGM/ranks/" .. f) )
		end
	else
		//Load the default ranks and create a new ranks.txt file for the owner to modify
		ParseRanks( file.Read("AGM/ranks.txt") )
		file.Write( "AGM/ranks/ranks.txt", file.Read("AGM/ranks.txt") )
	end
end
if SERVER then LoadRanks() end
function GetRankID( Rank )
	for i, v in pairs(Ranks) do if v.Title == Rank then return i end end
end
function Player:IsAdmin() return GetRankID( GetRank( self ) ) >= 2 end
function Player:IsSuperAdmin() return ( GetRankID( GetRank( self ) ) >= 3 end
function Player:IsOwner() return GetRankID( GetRank( self ) ) >= 4 end